layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage', 'upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;
	var where_json = {
		_o: 'goods_id',
		_b: 'desc'
	};

	var table_filter = 'mgoods_list',
		form_filter = 'mgoods_search_form';
	$('#my_search_btn').on('click', function() {
		layer.load();
		table.reload(table_filter, {
			where: where_json,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
	})
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: goods_mlist_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'goods_id',
			_b: 'desc'
		},
		cols: [goods_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 16,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#mgoodsToolbar',
		defaultToolbar: ['filter'],
		initSort: {
			field: 'goods_id',
			type: 'desc'
		},
		done: function() {
			upload.render({
				elem: '#upload_goods_template',
				url: goods_upload_url,
				auto: true,
				choose: function() {
					//layer.load(1);
				},
				acceptMime: 'application/vnd.ms-excel',
				done: function(res, index, upload) {
					if (res.code == 1) {
						layer.msg(res.msg, {
							icon: 1
						});
						table.reload(table_filter);
					} else {
						layer.open({
							title: '导入错误信息',
							content: res.msg,
							offset: 'rb',
							btn: [],
							closeBtn: 0,
							shadeClose: true,
							anim: 3
						});
					}
					layer.closeAll('loading');
				},
				exts: 'xls|xlsx'
			});
		}
	});
	table.on('checkbox(mgoods_list)', function(obj) {
		var checkStatus = table.checkStatus(table_filter);
		if (create_is_out == true) {
			checkStatus.data.forEach(function(e, i) {
				let temp = new Array();
				temp['goods_id'] = e.goods_id;
				temp['goods_name'] = e.goods_name;
				temp['goods_sku'] = e.goods_sku;
				temp['sale_price'] = e.sale_price;
				temp['api_price'] = e.api_price;
				temp['is_lock'] = e.is_lock;
				temp['sale_state'] = e.sale_state;
				temp['audit_state'] = e.audit_state;
				temp['view_stock'] = e.view_stock;
				bjd_data_arr.push(temp);
			})
			create_baojiadan_html();
		}
	})
	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		switch (obj.event) {
			case 'createExportCgGoods':
				$('#create_bjd_select_goods').show();
				create_is_out = true;
				break;
			case 'exportCgGoods':
				var checkStatus = table.checkStatus(table_filter);
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要导出的商品', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				layer.confirm('确定要导出选中产品吗？', {
					yes: function() {
						goods_arr = new Array();
						for (var i in checkStatus.data) {
							goods_arr.push(checkStatus.data[i].goods_sku);
						}
						var export_json = {
							goods_sku: goods_arr,
							_o: 'goods_id',
							_b: 'desc'
						};
						layer.load(2);
						send_info(goods_export_cg_url, export_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载excel',
								content: data.data,
								time: 500
							});
						})
					}
				});

				break;
			case 'exportGoods':
				var checkStatus = table.checkStatus(table_filter);
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要导出的商品', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				layer.confirm('确定要导出选中产品吗？', {
					yes: function() {
						goods_arr = new Array();
						for (var i in checkStatus.data) {
							goods_arr.push(checkStatus.data[i].goods_sku);
						}
						var export_json = {
							goods_sku: goods_arr,
							_o: 'goods_id',
							_b: 'desc'
						};
						layer.load(2);
						send_info(goods_export_url, export_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载excel',
								content: data.data,
								time: 500
							});
						})
					}
				});

				break;
			case 'transferGoods':
				var checkStatus = table.checkStatus(table_filter);
				var tr = checkStatus.tr;
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要转移的商品', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				layer.open({
					type: 1,
					title: '将商品转移到分类：',
					offset: ['200px', '100px'],
					area: ['800px', '465px'],
					closeBtn: false,
					shadeClose: true,
					btn: ['确定转移', '取消'],
					content: $('#transfer_select_category'),
					shade: 0,
					done: function() {
						console.info('完成');
					},
					yes: function() {
						var category_sign1 = $('select[name="transfer_category_sign1"]')
							.val(),
							category_sign2 = $('select[name="transfer_category_sign2"]')
							.val(),
							category_sign3 = $('select[name="transfer_category_sign3"]')
							.val(),
							goods_arr = new Array();
						for (var i in checkStatus.data) {
							goods_arr.push(checkStatus.data[i].goods_id);
						}
						if (category_sign1 == '' || category_sign2 == '' ||
							category_sign3 == '') {
							layer.msg('请选择要转移的分类', {
								icon: 2,
								anim: 6
							});
							return false;
						}
						send_info(goods_transfer_url, {
							category_sign1: category_sign1,
							category_sign2: category_sign2,
							category_sign3: category_sign3,
							goods_arr: goods_arr
						}, function(data) {
							if (data.code == 1) {
								layer.closeAll('page');
								layer.msg(data.msg, {
									icon: 1
								});
								table.reload(table_filter);
							} else {
								layer.msg(data.msg, {
									icon: 2
								});
							}
						})
						return false;
					}
				})
				break;
			case 'copyGoods':
				if (goods_select_site_id == '' && site_name == "主站") {
					layer.msg('请先从筛选框中选择要推送的分站', {
						icon: 2,
						anim: 6
					});
					$('form[lay-filter="goods_search_form"] select[name="site_id"]').trigger('click');
					$('form[lay-filter="goods_search_form"] select[name="site_id"]').next(
						'div.layui-form-select').addClass(
						'layui-form-selected')
					return false;
				}
				if (site_name == '主站') {
					title = '推送数据';
				} else {
					title = '采集数据';
				}
				layer.open({
					type: 2,
					anim: -1,
					title: title,
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: goods_copy_url + '?select_site_id=' + goods_select_site_id
				})
				break;
			case 'collectGoods':
				if (goods_select_site_id == '' && site_name == "主站") {
					layer.msg('请先从筛选框中选择要采集的分站', {
						icon: 2,
						anim: 6
					});
					$('form[lay-filter="goods_search_form"] select[name="site_id"]').trigger('click');
					$('form[lay-filter="goods_search_form"] select[name="site_id"]').next(
						'div.layui-form-select').addClass(
						'layui-form-selected')
					return false;
				}
				layer.open({
					type: 2,
					anim: -1,
					title: '采集分站商品',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: goods_collect_url + '?select_site_id=' + goods_select_site_id
				})
				break;
			case 'addGoods':
				create_tab($(this));
				break;
			case 'upGoods':
				var checkStatus = table.checkStatus(table_filter);
				var tr = checkStatus.tr;
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要上架的商品', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				var goods_id_arr = new Array();
				layer.confirm('确定上架选中商品吗？', function(index) {
					checkStatus.data.map(function(obj, i) {
						goods_id_arr.push(obj.goods_id);
					});
					send_info(goods_up_url, {
						goods_id: goods_id_arr
					}, function(data) {
						if (data.code == 0) {
							table.reload(table_filter);
							layer.msg('上架成功', {
								icon: 1
							});
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'downGoods':
				var checkStatus = table.checkStatus(table_filter);
				var tr = checkStatus.tr;
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要下架的商品', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				var goods_id_arr = new Array();
				layer.confirm('确定下架选中商品吗？', function(index) {
					checkStatus.data.map(function(obj, i) {
						goods_id_arr.push(obj.goods_id);
					});
					send_info(goods_down_url, {
						goods_id: goods_id_arr
					}, function(data) {
						if (data.code == 0) {
							table.reload(table_filter);
							layer.msg('下架成功', {
								icon: 1
							});
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'delGoods':
				var checkStatus = table.checkStatus(table_filter);
				var tr = checkStatus.tr;
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要删除的商品', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				var goods_id_arr = new Array();
				layer.confirm('确定删除选中商品吗？', function(index) {
					checkStatus.data.map(function(obj, i) {
						goods_id_arr.push(obj.goods_sku);
					});
					send_info(goods_delete_url, {
						goods_sku: goods_id_arr
					}, function(data) {
						if (data.code == 0) {
							tr.map(function(obj, i) {
								$(obj.selector).remove();
							})
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if ($('table[lay-filter="recycle_goods_list"]').length >
								0) {
								table.reload('recycle_goods_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			default:
				break;
		}
		return false;
	})

	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();

	// 下单时间
	var create_time_start = laydate.render({
		elem: 'input[name="goods_form_create_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('create_time_start', value);
		}
	});
	var create_time_end = laydate.render({
		elem: 'input[name="goods_form_create_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('create_time_end', value);
		}
	});
	var edit_brand = function(field, value, brand_sign) {
		var post = '{"brand_sign":"' + brand_sign + '","' + field + '":"' + value + '"}';
		var data = JSON.parse(post);
		send_info(brand_save_url, data, function(data) {
			if (data.code == -1) {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			} else {
				layer.msg('修改成功', {
					icon: 1
				});
			}
		})
	}
	//监听编辑
	table.on('edit(' + table_filter + ')', function(obj) {
		var field = obj.field,
			value = obj.value,
			brand_sign = obj.data.brand_sign;
		edit_brand(field, value, brand_sign);
	});
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var goods_sign = data.goods_sku;
		var goods_id = data.goods_id;
		switch (e) {
			case 'audit_log':
				layer.open({
					type: 2,
					anim: -1,
					title: '审核日志',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: true,
					area: '30%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: audit_log_url + '?audit_method=3&audit_sign=' + data.goods_sku +
						"&site_id=" + site_id
				})
				return false;
				break;
			case 'upGoods':
				layer.confirm('确定上架选中商品吗？', function(index) {
					send_info(goods_up_url, {
						goods_id: data.goods_id
					}, function(data) {
						if (data.code == 0) {
							table.reload(table_filter);
							layer.msg('上架成功', {
								icon: 1
							});
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'downGoods':
				layer.confirm('确定下架选中商品吗？', function(index) {
					send_info(goods_down_url, {
						goods_id: data.goods_id
					}, function(data) {
						if (data.code == 0) {
							table.reload(table_filter);
							layer.msg('下架成功', {
								icon: 1
							});
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'site_data':
				layer.open({
					type: 2,
					anim: -1,
					title: '分站价格',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: site_data_url + '?goods_sku=' + data.goods_sku
				})
				return false;
				break;
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {

					send_info(goods_delete_url, {
						goods_id: goods_id
					}, function(data) {
						if (data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if ($('table[lay-filter="recycle_goods_list"]').length >
								0) {
								table.reload('recycle_goods_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'edit':
				create_tab($(this));
				break;
			case 'update':
				send_info(brand_one_url, {
					brand_sign: data.brand_sign
				}, function(data) {
					delete data.data.brand_id;
					obj.update(data.data);
				})
				break;
			default:
				break;
		}
	});
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		where_json[name] = value;
		/* table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		}); */
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	});
	var select_search_list = function(field, value) {
		var where = {};
		where[field] = value;
		where_json[field] = value;
		/* table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		}); */
	};
	// 获取品牌
	var get_goods_brand_list = function(category_sign) {
		var e = $('form[lay-filter="' + form_filter + '"]').find('#brand_sign');
		e.html('');
		e.append('<option value="">加载中...</option>');
		e.prop('disabled', true);
		form.render('select');
		var post_data = {
			is_select: 'yes',
			category_sign: category_sign,
			_o: 'brand_id',
			_b: 'desc',
			page: 1,
			limit: 500
		};
		send_info(brand_select_uri, post_data, function(data) {
			e.html('<option value="">请选择</option>');
			if (data.count > 0) {
				for (var i in data.data) {
					var temp = data.data[i];
					if (goods_form_value.brand_sign == temp.brand_sign) {
						var op_html = '<option selected value="' + temp.brand_sign + '">' + temp
							.brand_name + '</option>';
					} else {
						var op_html = '<option value="' + temp.brand_sign + '">' + temp.brand_name +
							'</option>';
					}
					e.append(op_html);
				}
			};
			e.prop('disabled', false);
			form.render('select');
		})
	};
	var goods_form_value = {
		category_sign1: '',
		category_sign2: '',
		category_sign3: ''
	};
	// 初始化分类
	var get_goods_category_list_form = function(parent_sign, filter, value) {
		var e = $('form[lay-filter="goods_transfer_form"]').find('#' + filter);
		e.html('');
		e.append('<option value="">加载中...</option>');
		e.prop('disabled', true);
		var id = e.attr('id');
		form.render('select');
		var post_data = {
			parent_sign: parent_sign,
			audit_state: 1
		}
		send_info(category_select_uri, post_data, function(data) {
			e.html('<option value="">请选择分类2</option>');
			for (var i in data.data) {
				var temp = data.data[i];
				if (value == temp.category_sign) {
					var op_html = '<option selected value="' + temp.category_sign + '">' + temp
						.category_name + '</option>';
				} else {
					var op_html = '<option value="' + temp.category_sign + '">' + temp
						.category_name + '</option>';
				}
				e.append(op_html);
			}
			e.prop('disabled', false);
			form.render('select');
			if (value != undefined && value != '') {
				if (id == 'category_search1') {
					get_goods_category_list_form(value, 'category_search2', goods_form_value
						.category_sign2);
					$('form[lay-filter="goods_transfer_form"]').find('#category_search3').html('');
					$('form[lay-filter="goods_transfer_form"]').find('#goods_brand_search').html('');
				} else if (id == 'category_search2') {
					get_goods_category_list_form(value, 'category_search3', goods_form_value
						.category_sign3);
				} else if (id == 'category_search3') {}
			}
		})
	}
	// 获取品牌新版
	var get_goods_brand_div = function(brand_name) {
		var e = $('#select_brand_zixuan');
		e.html('');
		var post_data = {
			brand_name: brand_name,
			_o: 'brand_id',
			_b: 'desc',
			page: 1,
			limit: 30
		};
		send_info(brand_select_uri, post_data, function(data) {
			e.html('<dd class="dd-class" lay-value="">请选择</dd>');
			if (data.count > 0) {
				for (var i in data.data) {
					var temp = data.data[i];
					let op_html = '<dd class="dd-class" lay-value="'+temp.brand_sign+'">'+temp.brand_name+'</dd>'
					e.append(op_html);
				}
			};
		})
	}
	get_goods_brand_div('')
	// 搜索品牌
	$('#goods_brand_div_zixuan').on('input propertychange',function(){
		if ($(this).prop('comStart')) return;
		get_goods_brand_div($(this).val())
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	}).on('focus',function(){
		$('#select_brand_zixuan').show()
	}).on('blur',function(){
		setTimeout(function() {
			$('#select_brand_zixuan').hide()
		}, 150);
	})
	
	$('#select_brand_zixuan').on('click','.dd-class',function(){
		$('input[name="brand_sign"]').val($(this).attr('lay-value'))
		$('#goods_brand_div_zixuan').val($(this).text())
		select_search_list('brand_sign', $(this).attr('lay-value'));
	})
	// 新版品牌结束
	// 初始化分类
	var get_goods_category_list = function(parent_sign, filter, value) {
		var e = $('form[lay-filter="' + form_filter + '"]').find('#' + filter);
		e.html('');
		e.append('<option value="">加载中...</option>');
		e.prop('disabled', true);
		var id = e.attr('id');
		form.render('select');
		var post_data = {
			parent_sign: parent_sign,
			audit_state: 1
		}
		send_info(category_select_uri, post_data, function(data) {
			e.html('<option value="">请选择分类1</option>');
			for (var i in data.data) {
				var temp = data.data[i];
				if (value == temp.category_sign) {
					var op_html = '<option selected value="' + temp.category_sign + '">' + temp
						.category_name + '</option>';
				} else {
					var op_html = '<option value="' + temp.category_sign + '">' + temp
						.category_name + '</option>';
				}
				e.append(op_html);
			}
			e.prop('disabled', false);
			form.render('select');
			if (value != undefined && value != '') {
				if (id == 'category_search1') {
					get_goods_category_list(value, 'category_search2', goods_form_value
						.category_sign2);
					$('form[lay-filter="' + form_filter + '"]').find('#category_search3').html('');
					$('form[lay-filter="' + form_filter + '"]').find('#goods_brand_search').html('');
					//get_goods_brand_list(value);
				} else if (id == 'category_search2') {
					get_goods_category_list(value, 'category_search3', goods_form_value.category_sign3);
					//get_goods_brand_list(value);
				} else if (id == 'category_search3') {
					//get_goods_brand_list(value);
				}
			}
		})
	}
	var fliter1 = 'category_search1';
	// 分类联动
	form.on('select(list_category_search1)', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_category_list(value, 'category_search2', goods_form_value.category_sign2);
		$('form[lay-filter="' + form_filter + '"]').find('#category_search3').html('');
		$('form[lay-filter="' + form_filter + '"]').find('#brand_sign').html('');
		//get_goods_brand_list(value);
		select_search_list('category_sign1', value);
	})
	form.on('select(list_category_search2)', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_category_list(value, 'category_search3', goods_form_value.category_sign3);
		//get_goods_brand_list(value);
		select_search_list('category_sign2', value);
	})
	form.on('select(list_category_search3)', function(data) {
		var value = data.value;
		var e = data.elem;
		//get_goods_brand_list(value);
		select_search_list('category_sign3', value);
	})
	form.on('select(list_category_transfer1)', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_category_list_form(value, 'category_transfer2', value);
		$('form[lay-filter="' + form_filter + '"]').find('#category_transfer3').html('');
	})
	form.on('select(list_category_transfer2)', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_category_list_form(value, 'category_transfer3', value);
	})
	form.on('select(list_site_id)', function(data) {
		var value = data.value;
		var e = data.elem;
		goods_select_site_id = value;
		site_id = goods_select_site_id;
		select_search_list('site_id', value);
	})
	form.on('select(list_brand_sign)', function(data) {
		var value = data.value;
		var e = data.elem;
		select_search_list('brand_sign', value);
	})
	get_goods_category_list(0, 'category_search1', goods_form_value.category_sign1);
	//get_goods_category_list_form(0, 'category_transfer1');
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		select_search_list(name, value);
	});

})
